﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-04-02 14:53
    /// 描 述：人员报名名单服务类
    /// </summary>
    public class UserActivityRegistrationService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<UserActivityRegistrationEntity>> GetList(UserActivityRegistrationListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<UserActivityRegistrationEntity>> GetPageList(UserActivityRegistrationListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            if (expression==null) 
            {
                return null;
            }
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<UserActivityRegistrationEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<UserActivityRegistrationEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(UserActivityRegistrationEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<UserActivityRegistrationEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<UserActivityRegistrationEntity, bool>> ListFilter(UserActivityRegistrationListParam param)
        {
            var expression = LinqExtensions.True<UserActivityRegistrationEntity>();
            if (param != null)
            {
                if (param.newsid.IsNullOrZero())
                {
                    return null;
                }
                else
                {
                    expression = expression.And(t => t.newsid == param.newsid);
                }

                if (!param.username.IsEmpty())
                {
                    expression = expression.And(t => t.username.Contains(param.username));
                }

                if (!param.tenant_id.IsNullOrZero())
                {
                    expression = expression.And(t => t.tenant_id == param.tenant_id);
                }

                if (!param.ids.IsEmpty()) 
                {
                    long[] IdArr = TextHelper.SplitToArray<long>(param.ids, ',');
                    expression = expression.And(t => IdArr.Contains(t.Id.Value));
                }
            }
            return expression;
        }
        #endregion
    }
}
